<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/27
 * Time: 17:14
 */

namespace app\index\controller;


use think\Db;

class Shop extends Base{
    protected $userId;
    public function _initialize(){
        parent::_initialize();
        $this->userId=cookie('user_id');
    }
    // 旷工商城
    public function index(){
        $list=Db::name('worker')->select();
        $this->assign([
            'list' => $list,
        ]);
        return view();
    }
    // 旷工详情
    public function workerDetail(){
        $id=input('id');
        $worker=Db::name('worker')->where('id',$id)->find();
        $this->assign([
            'worker' => $worker,
        ]);
        return view('worker_detail');
    }
    // 用户购买旷工
    public function buyWorker(){
        $id = input('id');
        $worker=Db::name('worker')->where('id',$id)->find();
        $user=Db::name('users')->where('id',$this->userId)->find();
        if ($user['is_real'] == 0){
            return json(['msg' => '未实名,请实名后购买!','state' => -1]);
        }
        if ($user['money'] < $worker['money']){
            return json(['msg' => 'HX币余额不足' ,'state' => -1]);
        }
        // 限购次数验证
        $buyWorker=Db::name('user_worker')->where('user_id',$this->userId)->where('worker_id',$id)->count('id');
        if ($buyWorker+1 > $worker['limit_buy'] ){
            return json(['msg' => '该旷工已到购买上限','state' => -2]);
        }
        // 修改用户等级
        if ($user['level'] < $worker['tag']){
            Db::name('users')->where('id',$this->userId)->setField('level',$worker['tag']); // 用户升级了
        }
        # 1. 推广等级升级
        if ($user['p_id'] != 0){
            switch ($user['share_level']){
                case 0:
                    // 1.统计名下有多少人已经购买了旷工
                    if ($this->countLevel($user['p_id'],0)+1 >= 9){
                        $pUser=Db::name('users')->where('id',$user['p_id'])->field('share_level')->find();
                        if ($pUser['share_level'] == 0){
                            Db::name('users')->where('id',$user['p_id'])->setField('share_level',1);
                            Db::name('message')->insert(['title'=>'升级提醒','content' => '恭喜您升级为原始矿主','add_time'=>time(),'user_id'=>$user['p_id']]);
                        }
                    }
                    break;
                case 1:
                    if ($this->countLevel($user['p_id'],1) >= 1){
                        $pUser=Db::name('users')->where('id',$user['p_id'])->field('share_level')->find();
                        if ($pUser['share_level'] == 1){
                            Db::name('users')->where('id',$user['p_id'])->setField('share_level',2);
                            Db::name('message')->insert(['title'=>'升级提醒','content' => '恭喜您升级为奴隶矿主','add_time'=>time(),'user_id'=>$user['p_id']]);
                        }
                    }
                    break;
                case 2:
                    if ($this->countLevel($user['p_id'],2) >= 1){
                        $pUser=Db::name('users')->where('id',$user['p_id'])->field('share_level')->find();
                        if ($pUser['share_level'] == 2){
                            Db::name('users')->where('id',$user['p_id'])->setField('share_level',3);
                            Db::name('message')->insert(['title'=>'升级提醒','content' => '恭喜您升级为封建矿主','add_time'=>time(),'user_id'=>$user['p_id']]);
                        }
                    }
                    break;
                case 3:
                    if ($this->countLevel($user['p_id'],3) >= 1){
                        $pUser=Db::name('users')->where('id',$user['p_id'])->field('share_level')->find();
                        if ($pUser['share_level'] == 3){
                            Db::name('users')->where('id',$user['p_id'])->setField('share_level',4);
                            Db::name('message')->insert(['title'=>'升级提醒','content' => '恭喜您升级为半封建半殖民地矿主','add_time'=>time(),'user_id'=>$user['p_id']]);
                        }
                    }
                    break;
                case 4:

                    if ($this->countLevel($user['p_id'],4) >= 1){
                        $pUser=Db::name('users')->where('id',$user['p_id'])->field('share_level')->find();
                        if ($pUser['share_level'] == 4){
                            Db::name('users')->where('id',$user['p_id'])->setField('share_level',5);
                            Db::name('message')->insert(['title'=>'升级提醒','content' => '恭喜您升级为社会主义矿主','add_time'=>time(),'user_id'=>$user['p_id']]);
                        }
                    }
                    break;
                case 5:
                    if ($this->countLevel($user['p_id'],5) >= 2){
                        $pUser=Db::name('users')->where('id',$user['p_id'])->field('share_level')->find();
                        if ($pUser['share_level'] == 5){
                            Db::name('users')->where('id',$user['p_id'])->setField('share_level',6);
                            Db::name('message')->insert(['title'=>'升级提醒','content' => '恭喜您升级为HX推广者','add_time'=>time(),'user_id'=>$user['p_id']]);
                        }
                    }
                    break;
                case 6:
                    if ($this->countLevel($user['p_id'],6) >= 2){
                        $pUser=Db::name('users')->where('id',$user['p_id'])->field('share_level')->find();
                        if ($pUser['share_level'] == 6){
                            Db::name('users')->where('id',$user['p_id'])->setField('share_level',7);
                            Db::name('message')->insert(['title'=>'升级提醒','content' => '恭喜您升级为HX使者','add_time'=>time(),'user_id'=>$user['p_id']]);
                        }
                    }
                    break;
                case 7:
                    if ($this->countLevel($user['p_id'],7) >= 2){
                        $pUser=Db::name('users')->where('id',$user['p_id'])->field('share_level')->find();
                        if ($pUser['share_level'] == 7){
                            Db::name('users')->where('id',$user['p_id'])->setField('share_level',8);
                            Db::name('message')->insert(['title'=>'升级提醒','content' => '恭喜您升级为HX推广大使','add_time'=>time(),'user_id'=>$user['p_id']]);
                        }
                    }
                    break;
            }
        }
        #////////////////  2. 加速释放
        $pLists=$user['p_list'] == 0 ? [] : explode(',',$user['p_list']);
        foreach ($pLists as $key => $value){
            $ratio=0.01;
            if ($key == 0){ // 加速10%
                $ratio=0.1;
            }elseif($key == 1){// 加速5%
                $ratio=0.05;
            }elseif ($key > 1 && $key <=5){
                $ratio=0.01;
            }else{
                break;
            }
            //$firstUser=Db::name('users')->where('id',$value)->find(); // 查询出直推人的信息
            $speed=Db::name('user_worker')->where('user_id',$value)->sum('estimated_income'); // 查看出总加速的矿币
            $speeded=Db::name('user_worker')->where('user_id',$value)->sum('current_income');// 已经加速了矿币
            $speedScale=$worker['money']*$ratio; // speedScale的币数可以保证正常的加速
            if ($worker['money'] > $speed){
                $speedScale=($speed/2)*$ratio;
            }
            if (($speed-$speeded) < $speedScale){
                $speedScale=$speed-$speeded;// 只可以加速这么多
            }
            if (($speed - $speeded) > 0){
                $forSpeedScale=$speedScale; // 本次加速的币,该变量用于for循环
                $buyWorkers=Db::name('user_worker')->where('is_full=0')->where('user_id',$value)// 查询出用户购买了哪些旷工并按收益降序
                ->field('estimated_income-current_income as divide,id,worker_id')->order('current_income desc')->select();
                foreach ($buyWorkers as $k => $v){
                    if ($v['divide'] > 0 && $forSpeedScale > 0){
                        $speeds=$v['divide'];
                        if ($v['divide'] > $forSpeedScale){ // 可加速的大于本次加速的金额，怎么可加速的金额等于本次加速的金额
                            $speeds=$forSpeedScale;
                        }
                        Db::name('user_worker')->where('id',$v['id'])->setInc('current_income',$speeds);
                        Db::name('speed_reward')->insert(['user_id'=>$value,'add_time' => time(),'speed_reward'=>$speeds,
                            'speed_user_id'=>$this->userId, 'worker_id' => $v['worker_id']]);
                        $forSpeedScale-=$speeds;
                    }
                }
                Db::name('account_detail')->insert(['user_id' => $value, 'add_time' => time(), 'money' => $speedScale, 'remark' => '加速收益',]);
                Db::name('users')->where('id',$value)->setInc('money',$speedScale);
            }
        }
        #/////////////////////////////////
        # 3. 团队业绩
        Db::name('team_reward')->insert([
            'user_id' => $this->userId,'p_list' => $user['p_list'],'p_id' => $user['p_id'],'add_time' => time(),
            'reward' => $worker['money'],'worker_id' => $worker['id'],
        ]);
        #///////////////  把购买的旷工录入数据库中
        Db::name('user_worker')->insert([
            'user_id' => $this->userId, 'worker_id' => $id, 'add_time' => time(),
            'worker_num' => 1, 'estimated_income' => $worker['estimated_income'], 'make' => $worker['make'],
        ]);
        Db::name('users')->where('id',$this->userId)->setDec('money',$worker['money']); // 消费的记录
        Db::name('account_detail')->insert(['user_id' => $this->userId, 'add_time' => time(), 'money' => -$worker['money'], 'remark' => '购买旷机']);
        return json(['msg' => '旷机购买成功' ,'state' => 1]);
    }
    // 统计推荐人下面有多少对应的等级
    private function countLevel($pId,$level){
        return $count=Db::name('users')->where('p_id',$pId)->where('level','<>',$level)->count('id');
    }
}